/*
 *  Copyright (C) 2005-2007 Renier Roth
 *  www.renier.de
 *
 *  This program is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU General Public License
 *  as published by the Free Software Foundation; either version 2
 *  of the License, or any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */
package de.renier.jkeepass.actions;

import de.renier.jkeepass.JKeePass;
import de.renier.jkeepass.Messages;
import de.renier.jkeepass.elements.ContainerElement;
import de.renier.jkeepass.util.Utils;

import javax.swing.*;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreePath;
import java.awt.event.ActionEvent;
import java.io.File;
import java.io.FileOutputStream;

/**
 * ExportContainerAction
 *
 * @author <a href="mailto:software@renier.de">Renier Roth</a>
 */
public class ExportContainerAction extends AbstractAction {

  private static final long serialVersionUID = 970339428665321525L;

  public ExportContainerAction() {
    super(Messages.getString("ExportContainer.0"),new ImageIcon(ExportContainerAction.class.getResource("/org/javalobby/icons/20x20/SaveAs.gif"))); //$NON-NLS-1$ //$NON-NLS-2$
    this.setEnabled(false);
  }
  
  /* (non-Javadoc)
   * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
   */
  public void actionPerformed(ActionEvent e) {
    // get selected path
    TreePath treePath = JKeePass.application.getElementTreePanel().getLeadSelectionPath();
    if (treePath != null) {
      // get current selected
      DefaultMutableTreeNode node = (DefaultMutableTreeNode)treePath.getLastPathComponent();
      if (node.getUserObject() instanceof ContainerElement) {
        File saveFile = null;
        try {
          final JFileChooser fc = new JFileChooser();
          //fc.setFileFilter(Utils.getPassFileFilter());
          int ret = fc.showSaveDialog(JKeePass.application);
          if (ret == JFileChooser.APPROVE_OPTION) {
            saveFile = fc.getSelectedFile();
            // check if file exist
            if (saveFile.exists()) {
              // dialogbox to warn unsaved data
              int result = JOptionPane.showConfirmDialog(JKeePass.application,Messages.getString("ExportContainer.2") + saveFile.getPath() + Messages.getString("ExportContainer.3"), Messages.getString("ExportContainer.4") , JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
              if (result == JOptionPane.NO_OPTION) {
                return;
              }
            }
            // save file
            FileOutputStream outFile = new FileOutputStream(saveFile);
            // output
            Utils.outputNodeContainerUnencrypted(outFile, (ContainerElement)node.getUserObject());
            outFile.flush();
            outFile.close();
          }
        } catch (Exception ioe) {
          JOptionPane.showConfirmDialog(JKeePass.application,Messages.getString("ExportContainer.5") + saveFile != null ? saveFile.getPath() : "" + "\nError message: " + ioe.getMessage(), "Error" , JOptionPane.CLOSED_OPTION, JOptionPane.ERROR_MESSAGE); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
          ioe.printStackTrace();
        }
      }
    }
  }

}
